package com.hf.listener;

import java.util.HashMap;

import javax.servlet.http.HttpSession;

import com.base.bean.User;

/**
 * 参考：
 * http://zhaoshg.iteye.com/blog/791599
 * @author Administrator
 *
 */
public class MySessionContext {
	private static HashMap mymap = new HashMap();
	
	
	public static int getSessionCounts(){
		return mymap.size();
	}
	
	
	public static synchronized void AddSession(String openid,HttpSession session) {
        if (session != null) {
            mymap.put(openid, session);
        }
    }
	
//	//添加信息到session中  此方法无法实现不知道为什么
//	public static synchronized boolean saveUser(String openid,User user) {
//    try{
//    	HttpSession session=(HttpSession) mymap.get(openid);
//    	session.setAttribute("user", user);
//        mymap.put(openid, session);
//        return true;
//    }catch(Exception e){
//    	e.printStackTrace();
//    	return false;
//    }
//        
//    }
	
	
	
    public static synchronized void AddSession(HttpSession session) {
        if (session != null) {
            mymap.put(session.getId(), session);
        }
    }
    public static synchronized void DelSession(HttpSession session) {
        if (session != null) {
            mymap.remove(session.getId());
        }
    }
    public static synchronized HttpSession getSession(String session_id) {
    	HttpSession session=null;
    	
        if (session_id == null)
        	return null;
        try{
        	session=(HttpSession) mymap.get(session_id);
    
        	return session;
        }catch(Exception e){
        	return null;
        }
    }
}
